<?php

namespace App\Module\Record;

use App\Enum\CommonEnum;
use App\Module\BaseModule;
use App\Service\DictService;
use App\depository\RecordKillDep;
use App\depository\SteamIDEosidDep;

class KillModule extends BaseModule
{
    public function init($request)
    {
        $dictService = new DictService();
        $dictService->setTeams();

        $data = [
            'dict' => $dictService->getDict(),
        ];

        return self::response($data);
    }


    public function add($request)
    {
        $param = $request->input();
        $teamId = $request->headers->get('TeamId');

        $dep = new RecordKillDep();
        $mapDep = new SteamIDEosidDep();

        if($param['murderer_steamid']){
            $resMurdererMap = $mapDep->get_fields('eosid','steamid',$param['murderer_steamid']);
        }
        if($param['victim_steamid']){
            $resVictimMap = $mapDep->get_fields('eosid','steamid',$param['victim_steamid']);
        }
        $data = [
            'gh_id' => $teamId,
            'date' => $param['date'],

            'murderer' => $param['murderer'],
            'murderer_steamid' => $param['murderer_steamid'],
            'murderer_eosid' => $resMurdererMap ? $resMurdererMap->eosid :"",
            'hurt_type' => $param['hurt_type'],
            'hurt_num' => $param['hurt_num'],
            'victim' => $param['victim'],
            'victim_steamid' => $param['victim_steamid'],
            'victim_eosid' => $resVictimMap ? $resVictimMap->eosid : "",
            'weapon' => $param['weapon'],
        ];

        $dep->add($data);

        return self::response();
    }

    public function del($request)
    {
        $param = $request->input();

        $dep = new RecordKillDep();

        $dep->del($param['id']);
        return self::response();
    }

    public function edit($request)
    {
        $param = $request->input();

        $teamId = $request->headers->get('TeamId');

        $dep = new RecordKillDep();
        $mapDep = new SteamIDEosidDep();

        if($param['murderer_steamid']){
            $resMurdererMap = $mapDep->get_fields('eosid','steamid',$param['murderer_steamid']);
        }
        if($param['victim_steamid']){
            $resVictimMap = $mapDep->get_fields('eosid','steamid',$param['victim_steamid']);
        }
        $data = [
            'gh_id' => $teamId,
            'date' => $param['date'],

            'murderer' => $param['murderer'],
            'murderer_steamid' => $param['murderer_steamid'],
            'murderer_eosid' => $resMurdererMap ? $resMurdererMap->eosid :"",
            'hurt_type' => $param['hurt_type'],
            'hurt_num' => $param['hurt_num'],
            'victim' => $param['victim'],
            'victim_steamid' => $param['victim_steamid'],
            'victim_eosid' => $resVictimMap ? $resVictimMap->eosid : "",
            'weapon' => $param['weapon'],
        ];


        $dep->upd($param['id'],$data);

        return self::response();

    }

    public function list($request)
    {
        $param = $request->input();

        $teamId = $request->headers->get('TeamId');

        $param['gh_id'] = $teamId;
        $param['per_page'] = $param['per_page'] ?? 50;
        $param['current_page'] = $param['current_page'] ?? 1;

        $dep = new RecordKillDep();
        $resList = $dep->list($param);

        $data['list'] = $resList->map(function($item)  {
            return [
                'id' => $item->id,
                'gh_id' => $item->gh_id,
                'date' => $item->date,
                'murderer' => $item->murderer,
                'murderer_steamid' => $item->murderer_steamid,
                'murderer_eosid' => $item->murderer_eosid,
                'hurt_type' =>$item->hurt_type,
                'hurt_num' => $item->hurt_num,
                'victim' => $item->victim,
                'victim_steamid' => $item->victim_steamid,
                'victim_eosid' => $item->victim_eosid,
                'weapon' => $item->weapon,
            ];
        });

        $data['page'] = [
            'per_page' => $param['per_page'],
            'current_page' => $param['current_page'],
            'total_page' => $resList->lastPage(),
            'total' => $resList->total(),
        ];

        return self::response($data);
    }

}

